import * as cesium from 'cesium/Cesium'
const Cesium = cesium
export default class ConvertManager {
  constructor(oneGisCesium) {
    this.viewer = oneGisCesium.viewer
  }
  //笛卡尔转经纬度
  cartesian3ToDegrees = function (cartesian) {
    let cartographic = new Cesium.Cartographic.fromCartesian(cartesian);
    var longitude = Cesium.Math.toDegrees(cartographic.longitude);
    var latitude = Cesium.Math.toDegrees(cartographic.latitude);
    return {
      longitude: longitude,
      latitude: latitude,
      height: cartographic.height
    }
  }
  //经纬度转笛卡尔
  degreesToCartesian = function (degrees) {
    let result = new Cesium.Cartesian3.fromDegrees(
      degrees.longitude,
      degrees.latitude,
      degrees.height
    );
    return result
  }
   //根据经纬度求高度
   degreesToHeight = function (x, y) {
    let cartographic = Cesium.Cartographic.fromDegrees(x, y);
    let height = this.viewer.scene.globe.getHeight(cartographic);
    return height
  }
   //笛卡尔坐标转经纬度集合（turf使用）
   cartesian3ToTurfDegrees(cartesian) {
    let point = this.cartesian3ToDegrees(cartesian)
    return [point.longitude, point.latitude]
  }
   //笛卡尔坐标（无高度）添加高度
   cartesian3AddHeight = function (cartesian) {
    let point = this.cartesian3ToDegrees(cartesian)
    let height = this.degreesToHeight(point.longitude, point.latitude)
    point.height = height
    return this.degreesToCartesian(point)
  }
    //笛卡尔集合转经纬度集合
    cartesian3ToDegreesArray = function (cartesianArray) {
      let degreesPoints = []
      for (let i = 0; i < cartesianArray.length; i++) {
        let tempPoint = this.cartesian3ToDegrees(cartesianArray[i])
        degreesPoints.push(tempPoint);
      }
      return degreesPoints
    }
  //拾取坐标判断
  getPickPosition(position) {
    let viewer = this.viewer;
    let Cartesian2 = position;
    let model = viewer.scene.pick(Cartesian2);
    if (model instanceof Cesium.Cesium3DTileFeature) {
      let Cartesian3 = viewer.scene.pickPosition(Cartesian2)
      return Cartesian3
    } else {
      let ray = viewer.camera.getPickRay(Cartesian2)
      let Cartesian3 = viewer.scene.globe.pick(ray, viewer.scene);
      return Cartesian3
    }
  }
}